package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.linlinjava.litemall.db.dao.LitemallFormPurchaseItemMapper;
import org.linlinjava.litemall.db.dao.LitemallFormPurchaseMapper;
import org.linlinjava.litemall.db.dao.PurchaseMapper;
import org.linlinjava.litemall.db.domain.LitemallFormPurchase;
import org.linlinjava.litemall.db.domain.LitemallFormPurchaseExample;
import org.linlinjava.litemall.db.domain.LitemallFormPurchaseItem;
import org.linlinjava.litemall.db.domain.LitemallFormPurchaseItemExample;
import org.springframework.stereotype.Service;

@Service
public class PurchaseService {
    @Resource
    private PurchaseMapper purchaseMapper;
    @Resource
    private LitemallFormPurchaseMapper formPurchaseMapper;
    @Resource
    private LitemallFormPurchaseItemMapper formPurchaseItemMapper;

    public List<Map> getHerpTotalPurchases(String query,String orderByClause) {
        return purchaseMapper.selectHerpTotalPurchases(query, orderByClause);
    }

    public List<Map> getPurchases(String query,String orderByClause) {
        return purchaseMapper.selectPurchases(query, orderByClause);
    }

    public List<Map> getExtraPurchases(String query,Integer userId,String orderByClause) {
        return purchaseMapper.selectExtraPurchases(query,userId, orderByClause);
    }

    public LitemallFormPurchase query(Integer userId, Integer id) {
    	LitemallFormPurchaseExample example = new LitemallFormPurchaseExample();
        example.or().andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return formPurchaseMapper.selectOneByExample(example);
    }

    public int add(LitemallFormPurchase formPurchase) {
    	formPurchase.setAddTime(LocalDateTime.now());
    	formPurchase.setUpdateTime(LocalDateTime.now());
        return formPurchaseMapper.insertSelective(formPurchase);
    }

    public int update(LitemallFormPurchase formPurchase) {
    	formPurchase.setUpdateTime(LocalDateTime.now());
        return formPurchaseMapper.updateByPrimaryKeySelective(formPurchase);
    }

    public LitemallFormPurchaseItem queryByPurchaseIdAndItemId(Integer formPurchaseId, Integer id) {
    	LitemallFormPurchaseItemExample example = new LitemallFormPurchaseItemExample();
//        example.or().andFormPurchaseIdEqualTo(formPurchaseId).andIdEqualTo(id).andDeletedEqualTo(false);
        example.or().andFormPurchaseIdEqualTo(formPurchaseId).andIdEqualTo(id);
        return formPurchaseItemMapper.selectOneByExample(example);
    }
    
    public int update(LitemallFormPurchaseItem formPurchaseItem) {
    	formPurchaseItem.setUpdateTime(LocalDateTime.now());
        return formPurchaseItemMapper.updateByPrimaryKeySelective(formPurchaseItem);
    }
    
}
